<?php

namespace App\Http\Controllers\Admin;

use App\User;
use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
require_once app_path().'/Http/Org/code/Code.class.php';
use App\Http\Org\code\Code;
use App\models\Permission;

use Gregwar\Captcha\CaptchaBuilder;
use Gregwar\Captcha\PhraseBuilder;

use Illuminate\Support\Facades\Crypt;
use Illuminate\Support\Facades\Hash;
use Validator;

use App\models\auser;
use App\models\Role;


class LoginController extends Controller
{
	/*
 	 *返回后台登录视图
 	 *@param
 	 *@return 登录视图
 	 *@author 肖峰
 	 *@date 2017/10/30 20:13
 	 */
    public function login()
    {
        return view('admin.login.login');
    }

    // 测试
    public function test()
    {
    	$users = auser::all();
    	dd($users);
    }
    // 返回验证码
    public function yzm()
    {
    	$code = new Code;
    	$code ->make();
    }

    // 登录处理
    public function doLogin(Request $request)
    {
    	//1.接收用户提交的登录数据
    	$input = $request->except('_token');
        // dd($input);
    	// dd($input);

    	//2.验证数据的有效性
    	// $validator = Validator::make(要验证的数据,验证规则,提示信息);

        // 如何判断密码和确认密码一致 password repassword    

        // $rule = [
        //     'username' => 'required|between:4,18',
        //     'password' => 'required|between:4,18'
        //     'repassword' => 'required|same:password'
        // ];
        



    	$rule = [
    		'username' => 'required|between:4,18',
    		'password' => 'required|between:4,18'
    	];

    	$msg = [
            'username.required'=>'必须输入用户名',
            'username.between'=>'用户名必须在4-18位之间',
            'password.required'=>'必须输入密码',
            'password.between'=>'密码必须在4-18位之间',
        ];





        	$validator = Validator::make($input,$rule,$msg);
        	// 如果验证失败
        	  if ($validator->fails()) {
                return redirect('admin/login')
                    ->withErrors($validator)
                    ->withInput();
            }

            
        	//3.登录的逻辑判断
        		// 3.1 验证码是否正确
        		if(strtoupper($input['code']) != session('code')){
        			return redirect('admin/login')->with('errors','验证码错误')->withInput();
        		}

        		// 3.2 是否有此用户
                $user = auser::where('username',$input['username'])->first();
                // dd($user);
                if(!$user){
                    return redirect('admin/login')->with('errors','无此用户')->withInput();
                }

                // dd(Hash::check($input['password'],$user->password));
        		// 3.3 密码是否正确
                if(!Hash::check($input['password'],$user->password)){
                    return redirect('admin/login')->with('errors','密码错误')->withInput();
                }
        	// 4.如果登录成功,跳转到后台首页,如果登录失败,跳回到登录页
                session(['user'=>$user]);

                $qx = Permission::all();
                $_qx = [];
                foreach($qx as $k=>$v)
                {
                    $_qx[] = $v['pname'];
                }

                // dd($_qx);
                $auser = auser::find(session('user')->id);

                $role = $auser->role;

                $roles = [];
                foreach($role as $a => $b){
                    $roles[] = $b -> id;
                }

                $per = [];
                foreach($roles as $c => $d){
                    $per[] = Role::find($d)->permission;
                }
                

                // dd($per);

                if($per){
                    $pers = [];
                    foreach($per as $k => $v){
                        foreach($v as $q => $w){
                            $pers[] = $w -> pname;
                        }
                    }

                // dd($pers);
                $new_per = array_unique($pers);
                $new_per = array_diff($_qx,$new_per);

                // dd($new_per);
                session(['perm'=>$new_per]);

                // dd($pers);


                }else{
                    session(['perm'=>$_qx]);
                }

                

                


                // 跳转到后台首页
                  return redirect('admin/index');
    }



    // hash 生成加密密码   $2y$10$ly7Gm.bLfslpLb/v44nIveP6X73Jq3.7yiSMNVOAlkAyLtsEPdn7e
    // admin               $2y$10$CZp6Lr4h3672uicIOxYDb./uGZF1kedB7ZdUQeAL9YezzPt4EWl8q
    public function jiami()
    {
        

        $str = '1234567';
        $encrypt = Hash::make('123456');
        
        dd($encrypt);

    }
}
